package com.soft.line.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.soft.line.entity.ContractInfo;
import com.soft.line.filter.HttpRequestUtil;
import com.soft.line.filter.UserTokenDto;
import com.soft.line.mapper.AttachmentInfoMapper;
import com.soft.line.mapper.ContractInfoMapper;
import com.soft.line.service.ContractInfoService;
import com.soft.line.util.pagehelper.PageUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wu.xueqiao on 2020/5/21.
 * @version 0.1
 */
@Service
@Slf4j
public class ContractInfoServiceImpl implements ContractInfoService{
    @Autowired
    private ContractInfoMapper contractInfoMapper;
    @Autowired
    private AttachmentInfoMapper attachmentInfoMapper;


    @Override
    public PageInfo<ContractInfo> getAllAboutContracts(Map<String, Object> map) {
        PageUtil.setPageParam(map);
        List<ContractInfo> contractInfos=contractInfoMapper.getAllAboutContracts(map);
        PageInfo<ContractInfo> beans = new PageInfo(contractInfos);
        beans.setList(contractInfos);
        return beans;
    }

    @Override
    public PageInfo<ContractInfo> getAllContracts(Map<String, Object> map) {
        //PageUtil.setPageParam(map);
        List<ContractInfo> conResults = new ArrayList<ContractInfo>();
        //根据查询条件查询数据
        List<ContractInfo> constracts1 = contractInfoMapper.selectAll(map);
        if (constracts1.size() == 0) {
            conResults = constracts1;
        } else {
            List<Integer> idList = new ArrayList<Integer>();
            for (ContractInfo con : constracts1) {
                idList.add(con.getId());
            }
            //查到的id在关联pro_id查询对应的收入支出合同
            List<ContractInfo> constracts2 = contractInfoMapper.selectAllNaturesByIdList(idList);
            for (ContractInfo con : constracts2) {
                idList.add(con.getId());
            }
            map.put("idList", idList);
            PageUtil.setPageParam(map);
            conResults = contractInfoMapper.getAllByIdList(map);
        }
        PageInfo<ContractInfo> beans = new PageInfo(conResults);
        beans.setList(conResults);
        return beans;
    }

    @Override
    public void delete(Map<String, Object> map) {
        contractInfoMapper.delete(map);
        attachmentInfoMapper.delete("1",(Integer)map.get("id"));
    }

    @Override
    public void add(ContractInfo contractInfo) {
        String userName="admin";
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            userName=userToken.getUserInfo().getUserName();
        }
        contractInfo.setModifyPerson(userName);
        contractInfoMapper.insert(contractInfo);
    }

    @Override
    public void update(ContractInfo contractInfo) {
        String userName="admin";
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            userName=userToken.getUserInfo().getUserName();
        }
        contractInfo.setModifyPerson(userName);
        contractInfo.setModifyTime(new Date());
        contractInfoMapper.update(contractInfo);
    }

    @Override
    public void insertList(List<ContractInfo> list) {
        String userName="admin";
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            userName=userToken.getUserInfo().getUserName();
        }
        for (ContractInfo record:list){
            record.setModifyPerson(userName);
            record.setModifyTime(new Date());
            contractInfoMapper.insertRecord(record);
        }
    }

    @Override
    public int countContractByIdAndName(Integer id, String contractName) {
        return contractInfoMapper.countContractByIdAndName(id, contractName);
    }

    @Override
    public List<ContractInfo> getAllCNByIDList(Map<String, Object> map) {
        return contractInfoMapper.getAllCNByIdList((List<Integer>)map.get("ids"));
    }
}
